﻿namespace Compass.Wpf.ViewModels;

public class FinishGoodsCountsViewModel : NavigationViewModel
{
    #region ctor
    private readonly IFinishGoodsCountService _finishGoodsCountService;
    private readonly IPrintsService _printsService;
    public FinishGoodsCountsViewModel(IContainerProvider provider) : base(provider)
    {

        _finishGoodsCountService = provider.Resolve<IFinishGoodsCountService>();
        _printsService=provider.Resolve<IPrintsService>();

        ExecuteCommand = new DelegateCommand<string>(Execute);
        UpdateItem = new DelegateCommand<FinishedGoodsCountDto>(UpdateFinishedGoodsCount);
        DeleteItem = new DelegateCommand<FinishedGoodsCountDto>(DeleteFinishedGoodsCount);
        UpdateRoles = "admin,whse";
        IsEnable = true;
    }
    public DelegateCommand<string> ExecuteCommand { get; }
    public DelegateCommand<FinishedGoodsCountDto> UpdateItem { get; }
    public DelegateCommand<FinishedGoodsCountDto> DeleteItem { get; }
    #endregion

    #region 角色控制属性
    private string updateRoles;
    public string UpdateRoles
    {
        get => updateRoles;
        set { updateRoles = value; RaisePropertyChanged(); }
    }
    #endregion

    #region 属性
    private bool isEnable;
    public bool IsEnable
    {
        get => isEnable;
        set { isEnable = value; RaisePropertyChanged(); }
    }
    private bool showAll;
    public bool ShowAll
    {
        get => showAll;
        set
        {
            SetProperty(ref showAll, value);
            GetDataAsync();
        }
    }
    public ObservableRangeCollection<FinishedGoodsCountDto> FinishedGoodsCountDtos { get; } = new();
    #endregion

    private async void Execute(string obj)
    {
        switch (obj)
        {
            case "Add": Add(); break;
            case "ExportFinishedGoodsInfo": await ExportFinishedGoodsInfo(); break;
        }
    }



    private async void Add()
    {
        //弹出修改界面
        var obj = new FinishedGoodsCountDto()
        {
            FinishTime= DateTime.Now,
            WarehousingTime= DateTime.Now,
            ShippingTime=DateTime.Now,
            OutsideTime=DateTime.Now,
            CanEdit=true
        };
        var dialogParams = new DialogParameters { { "Value", obj } };
        var dialogResult = await DialogHost.ShowDialog(nameof(AddFinishGoodsCountView), dialogParams);
        if (dialogResult.Result != ButtonResult.OK) return;

        var newItem = dialogResult.Parameters.GetValue<FinishedGoodsCountDto>("Value");
        if (!newItem.Shipping) newItem.ShippingTime = null;
        if (!newItem.Outside) newItem.OutsideTime = null;

        await _finishGoodsCountService.AddAsync(newItem);

        GetDataAsync();
    }

    private async void UpdateFinishedGoodsCount(FinishedGoodsCountDto obj)
    {
        //弹出修改界面
        if (!obj.CanEdit) return;
        obj.Shipping = obj.ShippingTime!=null;
        obj.Outside = obj.OutsideTime!=null;

        var dialogParams = new DialogParameters { { "Value", obj } };
        var dialogResult = await DialogHost.ShowDialog(nameof(AddFinishGoodsCountView), dialogParams);
        if (dialogResult.Result != ButtonResult.OK) return;
        var newItem = dialogResult.Parameters.GetValue<FinishedGoodsCountDto>("Value");
        if (!newItem.Shipping) newItem.ShippingTime = null;
        if (!newItem.Outside) newItem.OutsideTime = null;

        await _finishGoodsCountService.UpdateAsync(obj.Id.Value, newItem);

        GetDataAsync();
    }

    private async void DeleteFinishedGoodsCount(FinishedGoodsCountDto obj)
    {
        //删除询问
        if (!obj.CanEdit) return;
        var dialogResult = await DialogHost.Question("删除确认", $"确认删除项目：{obj.OdpNumber} {obj.Name} 吗?");
        if (dialogResult.Result != ButtonResult.OK) return;
        await _finishGoodsCountService.DeleteAsync(obj.Id.Value);

        FinishedGoodsCountDtos.Remove(obj);
    }



    private async Task ExportFinishedGoodsInfo()
    {
        IsEnable = false;
        await _printsService.ExportFinishedGoodsInfoAsync(FinishedGoodsCountDtos.ToList());
        IsEnable = true;
    }
    #region 初始化
    private async void GetDataAsync()
    {
        FinishedGoodsCountDtos.ReplaceRange((await _finishGoodsCountService.GetAllWithFeeAsync(ShowAll)).Result);
    }

    public override void OnNavigatedTo(NavigationContext navigationContext)
    {
        base.OnNavigatedTo(navigationContext);
        GetDataAsync();
    }
    #endregion
}